var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'title',
        title: '标题',
        type: 'string',
        headerClass: 'text-left width-200',
        columnClass: 'text-left width-200',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false,
        hideType: 'sm|xs'
    },
    {
        id: 'author',
        type: 'string',
        title: '作者',
        headerClass: 'text-left width-100',
        columnClass: 'text-left width-100',
        fastQuery: true,
        fastSort: false,
        fastQueryType: 'eq'
    },
    {
        id: 'sortNo',
        title: '排序',
        type: 'string',
        headerClass: 'text-left width-50',
        columnClass: 'text-left width-50',
        fastQuery: false,
        fastQueryType: 'eq',
        fastSort: false,
        hideType: 'sm|xs'
    },
    // {
    //     id: 'createTime',
    //     title: '添加时间',
    //     type: 'date',
    //     format: 'yyyy-MM-dd',
    //     headerClass: 'text-left width-200',
    //     columnClass: 'text-left width-200',
    //     fastQuery: true,
    //     fastQueryType: 'range',
    //     hideType: 'md|sm|xs'
    // },
    {
        id: 'updateTime',
        title: '发布时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left width-200',
        columnClass: 'text-left width-200',
        fastQuery: true,
        fastQueryType: 'range',
        hideType: 'md|sm|xs'
    },
    {
        id: 'status',
        title: '状态',
        type: 'string',
        columnClass: 'text-center width-150',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        hideType: 'md|sm|xs',
        codeTable: {
            1: $lang.articleStatus.WAIT,
            2: $lang.articleStatus.PUBLISHED,
            3: $lang.articleStatus.BACK
        }
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-400',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html, edit, view, publish, back, del;
            if ($("#informationEdit") && $("#informationEdit").val() == 1) {
                edit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            }

            if ($("#informationPublish") && $("#informationPublish").val() == 1) {
                publish = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.pubInfo(" + record.id + ",\"" + record.title + "\")'><i class='fa fa-paper-plane-o'></i>&nbsp;发布&nbsp;</a>";
            }

            if ($("#informationBack") && $("#informationBack").val() == 1) {
                back = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.backInfo(" + record.id + ",\"" + record.title + "\")'><i class='fa fa-reply-all'></i>&nbsp;撤回&nbsp;</a>";
            }

            if ($("#informationDel") && $("#informationDel").val() == 1) {
                del = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.delInfo(" + record.id + ",\"" + record.title + "\")'><i class='fa fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            }

            if ($("#informationView") && $("#informationView").val() == 1) {
                view = "<a data-target='#viewModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-eye'></i>&nbsp;预览&nbsp;</a>";
            }

            if (record.status != 2) {
                html = edit + publish + del + view;
            } else if (record.status == 2) {
                html = back + view;
            }

            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'information/json/list',
    exportFileName: '资讯列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id", "sortNo", "delFlag"], Timestamp: ["createTime"]},
    onGridComplete: function (grid) {
    }
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.sortParameter.columnId = ['desc_createTime'];
// 默认查询条件
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;

var information = function () {

    /**
     * 图片缩放
     */
    function _refreshImage() {
        $(".viewImage").jqthumb({
            width: 160,
            height: 100,
            after: function (imgObj) {
                imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
            }
        });
    }


    //编辑器对象[添加]
    var _ueAdd;
    //编辑器对象[编辑]
    var _ueEdit;

    /**
     * 删除
     */
    function _delInfo(id, content) {
        var tpl = '您选择对资讯 <strong>' + content + '</strong> 进行删除操作，删除后将无法恢复。<br/>您确定要进行删除操作吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "information/json/del",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "删除操作"
        });
    }

    /**
     * 撤回
     */
    function _backInfo(id, content) {
        var tpl = '您是否确定撤回资讯 <strong>' + content + '</strong> ?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "information/json/back",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "撤回操作"
        });
    }

    /**
     * 发布
     */
    function _pubInfo(id, content) {
        var tpl = '您是否确定发布资讯 <strong>' + content + '</strong> ?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "information/json/publish",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "发布操作"
        });
    }

    var addInfo = {
        initAddModal: function () {
            $addForm = $("#addForm");
            $addForm.find('input[name="title"]').val("");
            $addForm.find('input[name="sortNo"]').val("");
            $addForm.find('input[name="author"]').val("");
            $('#addFormPicPath').val("");
            $('#addFormPicImg').attr('src', ncGlobal.imgRoot+"default_image.gif");
            $('#addModal').find('div[name="preview"]').html('');
            information.ueAdd.setContent("");
            $(".alert-danger").remove();
            $addForm.psly().reset();
        }
    }

    /**
     * 事件绑定
     * @private
     */
    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters['lk_title'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
        //图片上传
        $("#addFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#addFormPicPath').val(data.result.data);
                    $('#addFormPicImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-新增
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        /**
         * 上传插件绑定
         */
        $("#editFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                    }
                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#editFormPicPath').val(data.result.data);
                    $('#editFormPicImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-编辑
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        // 新增资讯对话框
        $("#addForm").on("nc.formSubmit.success", function (e) {
            addInfo.initAddModal();
            _refreshImage();
        })
        //添加微信事件时显示时
        $('#addModal').on('show.bs.modal', function (event) {
            addInfo.initAddModal();
            _refreshImage();
        });
        // bycj [ 编辑对话框显示时调用 ]
        $('#editModal').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
            modal.find('input[name="id"]').val(gridData.id);
            modal.find('input[name="title"]').val(gridData.title);
            modal.find('input[name="sortNo"]').val(gridData.sortNo);
            modal.find('input[name="author"]').val(gridData.author);
            $('#editFormPicPath').val(gridData.picPath);
            $('#editFormPicImg').attr('src', ncGlobal.fileRoot + gridData.picPath);
            $('#editModal').find('div[name="preview"]').html('');
            _refreshImage();
            if (gridData.content != null) {
                information.ueEdit.setContent(gridData.content);
            }

            //清除错误提示
            editForm.psly().reset();
            $(".alert-danger").remove()
        });
        // 图片上传
        $("#addFormFile").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                    }
                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    var html = '<span class="col-md-3 col-sm-6 m-b-10"><img class="viewImage" src="' + ncGlobal.fileRoot + data.result.data + '"><a name="insert" href="javascript:;" class="btn btn-success btn-xs m-t-5"><i class="fa fa-arrow-circle-o-up"></i>&nbsp;插入</a><a name="delete" href="javascript:;" class="btn btn-danger btn-xs m-t-5 m-l-5"><i class="fa fa-trash-o"></i>&nbsp;删除</a></span>';
                    $('#addModal').find('div[name="preview"]').append(html);
                    var item = $('#addModal').find('div[name="preview"] > span').last();
                    $(item).find('a[name="insert"]').on('click', function () {
                        information.ueAdd.execCommand('inserthtml', '<img src="' + ncGlobal.fileRoot + data.result.data + '">');
                    });
                    $(item).find('a[name="delete"]').on('click', function () {
                        $(this).parents('span').remove();
                    });
                    //图片同比例缩放-默认
                    $('.information-pic img').jqthumb({
                        width: 100,
                        height: 100,
                        after: function (imgObj) {
                            imgObj.css('opacity', 0).animate({opacity: 1}, 2000);
                        }
                    });
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        // 图片上传
        $("#editFormFile").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                    }
                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    var html= '<span class="col-md-3 col-sm-6 m-b-10"><img class="viewImage" src="' + ncGlobal.fileRoot + data.result.data + '"><a name="insert" href="javascript:;" class="btn btn-success btn-xs m-t-5"><i class="fa fa-arrow-circle-o-up"></i>&nbsp;插入</a><a name="delete" href="javascript:;" class="btn btn-danger btn-xs m-t-5 m-l-5"><i class="fa fa-trash-o"></i>&nbsp;删除</a></span>';
                    $('#editModal').find('div[name="preview"]').append(html);
                    var item = $('#editModal').find('div[name="preview"] > span').last();
                    $(item).find('a[name="insert"]').on('click', function () {
                        information.ueEdit.execCommand('inserthtml', '<img src="' + ncGlobal.fileRoot + data.result.data + '">');
                    });
                    $(item).find('a[name="delete"]').on('click', function () {
                        $(this).parents('span').remove();
                    });
                    $('.information-pic img').jqthumb({
                        width: 100,
                        height: 100,
                        after: function (imgObj) {
                            imgObj.css('opacity', 0).animate({opacity: 1}, 2000);
                        }
                    });
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
    }

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        },
        ueAdd: _ueAdd,
        ueEdit: _ueEdit,
        delInfo: _delInfo,
        backInfo: _backInfo,
        pubInfo: _pubInfo
    }
}();

$(function () {
    grid.load();
    information.init();
    //实例化编辑器
    information.ueAdd = UE.getEditor('contnetAdd', {textarea: 'content'});
    information.ueEdit = UE.getEditor('contnetEditor', {textarea: 'content'});
})